// +----------------------------------------------------------------------
// | huang [ 美业管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 psi All rights reserved.
// +----------------------------------------------------------------------
// | Author: huang <2374266244@qq.com>
// +----------------------------------------------------------------------
// +----------------------------------------------------------------------
// | 功能: 跨店消费类
// +----------------------------------------------------------------------
'use strict';
import Base from './base.js';

export default class extends Base {
    /*
     * 获取请求店的跨店消费情况（其他店顾客在请求店消费）
     */
    async getOtherShopGuestOrderAction() {
        // 查询所有订单
        let data = this.get()
        let time = data.times ? JSON.parse(data.times) : ''
        let where = {}
        if (time && (time[0] != null && time[1] != null)) {
            let begintime = Math.floor(new Date(time[0]).setHours(0, 0, 0, 0) / 1000)
            let endtime = Math.floor(new Date(time[1]).setHours(0, 0, 0, 0) / 1000) + 86400 // 当天加上一天
            where['o.addtime'] = { '>=': begintime, '<': endtime }
        }
        where['o.status'] = ['!=', 0]
        where['o.departmentid'] = this.shop_userinfo.departmentid
        where['g.departmentid'] = ['not in', [0, this.shop_userinfo.departmentid]]
        if (data.shopId != 0) {
            where['d.id'] = data.shopId
        }
        if (data.orderType != 0) {
            where['o.ordertype'] = data.orderType
        }

        let orderList = await this.model('or_shop_order')
            .alias('o')
            .field('o.*, g.name, g.phone, g.vipcard, g.headurl, d.id as shopid, d.name as shopname')
            .join('`think_or_guest` g on o.guestid = g.id')
            .join('`think_or_department` d on g.departmentid = d.id')
            .where(where)
            .page(data.page, data.limit)
            .countSelect()
        return this.json(orderList)
    }

    /*
     * 获取请求店的跨店消费情况（当前店顾客在其他店消费）
     */
    async getGuestOrderInOtherShopAction() {
        let data = this.get()
        let time = data.times ? JSON.parse(data.times) : ''
        let where = {}
        if (time && (time[0] != null && time[1] != null)) {
            let begintime = Math.floor(new Date(time[0]).setHours(0, 0, 0, 0) / 1000)
            let endtime = Math.floor(new Date(time[1]).setHours(0, 0, 0, 0) / 1000) + 86400 // 当天加上一天
            where['o.addtime'] = { '>=': begintime, '<': endtime }
        }
        where['o.status'] = ['!=', 0]
        where['g.departmentid'] = this.shop_userinfo.departmentid
        where['o.departmentid'] = ['!=', this.shop_userinfo.departmentid]
        if (data.shopId != 0) {
            where['d.id'] = data.shopId
        }
        if (data.orderType != 0) {
            where['o.ordertype'] = data.orderType
        }

        let orderList = await this.model('or_shop_order')
            .alias('o')
            .field('o.*, g.name, g.phone, g.vipcard, g.headurl, d.id as shopid, d.name as shopname')
            .join('`think_or_guest` g on o.guestid = g.id')
            .join('`think_or_department` d on o.departmentid = d.id')
            .where(where)
            .page(data.page, data.limit)
            .countSelect()
        return this.json(orderList)
    }

    /*
     * 获取所有分店列表
     */
    async getOtherShopListAction() {
        let pid = await this.model('or_department').where({ id: this.shop_userinfo.departmentid }).getField('pid', true)
        let shopList = await this.model('or_department').where({ pid: pid, id: ['!=', this.shop_userinfo.departmentid] }).select()
        return this.json(shopList)
    }

    async getOtherShopGuestOrderInfoAction() {
        let data = this.get()
        let time = data.times ? JSON.parse(data.times) : ''
        let where = {}
        if (time && (time[0] != null && time[1] != null)) {
            let begintime = Math.floor(new Date(time[0]).setHours(0, 0, 0, 0) / 1000)
            let endtime = Math.floor(new Date(time[1]).setHours(0, 0, 0, 0) / 1000) + 86400 // 当天加上一天
            where['o.addtime'] = { '>=': begintime, '<': endtime }
        }
        where['o.status'] = ['!=', 0]
        where['o.departmentid'] = this.shop_userinfo.departmentid
        where['g.departmentid'] = ['not in', [0, this.shop_userinfo.departmentid]]
        if (data.shopId != 0) {
            where['d.id'] = data.shopId
        }
        if (data.orderType != 0) {
            where['o.ordertype'] = data.orderType
        }

        let orderList = await this.model('or_shop_order')
            .alias('o')
            .field('d.id as shopId, d.name as shopName, sum(if(o.ordertype=3,o.recemoney,0)) as czMoney, sum(o.recemoney) as receMoney, sum(o.cardreduce) as cardReduce, sum(o.usebalance) as useBalance, sum(o.usegiftbalance) as useGiftBalance')
            .join('`think_or_guest` g on o.guestid = g.id')
            .join('`think_or_department` d on g.departmentid = d.id')
            .where(where)
            .group('d.id')
            .select()
        return this.json(orderList)
    }

    // 获取本店客户到其他店的合计信息
    async getGuestOrderInOtherShopInfoAction() {
        let data = this.get()
        let time = data.times ? JSON.parse(data.times) : ''
        let where = {}
        if (time && (time[0] != null && time[1] != null)) {
            let begintime = Math.floor(new Date(time[0]).setHours(0, 0, 0, 0) / 1000)
            let endtime = Math.floor(new Date(time[1]).setHours(0, 0, 0, 0) / 1000) + 86400 // 当天加上一天
            where['o.addtime'] = { '>=': begintime, '<': endtime }
        }
        where['o.status'] = ['!=', 0]
        where['g.departmentid'] = this.shop_userinfo.departmentid
        where['o.departmentid'] = ['!=', this.shop_userinfo.departmentid]
        if (data.shopId != 0) {
            where['d.id'] = data.shopId
        }
        if (data.orderType != 0) {
            where['o.ordertype'] = data.orderType
        }

        let orderList = await this.model('or_shop_order')
            .alias('o')
            .field('d.id as shopId, d.name as shopName, sum(if(o.ordertype=3,o.recemoney,0)) as czMoney, sum(o.recemoney) as receMoney, sum(o.cardreduce) as cardReduce, sum(o.usebalance) as useBalance, sum(o.usegiftbalance) as useGiftBalance')
            .join('`think_or_guest` g on o.guestid = g.id')
            .join('`think_or_department` d on o.departmentid = d.id')
            .where(where)
            .group('d.id')
            .select()
        return this.json(orderList)
    }
}